package com.km.sglototo.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.km.sglototo.annotation.SysLog;
import com.km.sglototo.sys.entity.SysNotice;
import com.km.sglototo.sys.entity.SysUser;
import com.km.sglototo.sys.service.SysNoticeService;
import com.km.sglototo.utils.CommonResult;
import com.km.sglototo.utils.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author xt
 * @since 2022-04-06
 */
@RestController
@RequestMapping("/sysNotice")
public class SysNoticeController {

    @Resource
    SysNoticeService sysNoticeService;

    @SysLog(MODULE = "sysNotice", REMARK = "根据条件查询所有公告列表")
    @ApiOperation("根据条件查询所有用户列表")
    @GetMapping(value = "/list")
    public Object getUserByPage(SysNotice entity,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            entity=null;
            QueryWrapper qw = new QueryWrapper<>(entity);
            // // qw.eq("username","admin");
            // qw.orderByDesc("id");
            // qw.eq("status",1);
            Page ps = new Page<SysUser>(pageNum, pageSize);
            return new CommonResult().success(sysNoticeService.page(ps, qw));
        } catch (Exception e) {
            // log.error("根据条件查询所有用户列表：%s", e.getMessage(), e);
        }
        return new CommonResult().failed();
    }


    @SysLog(MODULE = "sysNotice", REMARK = "保存通知")
    @ApiOperation("保存通知")
    @PostMapping(value = "/register")
    public Object saveSysNotice(@RequestBody SysNotice entity) {
        try {

            if(entity.getId()!=null){
                boolean is_succ = sysNoticeService.updateById(entity) ;
                return new CommonResult().success();
            }else {
                 boolean is_succ = sysNoticeService.save(entity);
                return new CommonResult().success();
            }

            // if (sysUserService.saveOrUpdate(entity)) {
            //     return new CommonResult().success();
            // }
        } catch (Exception e) {
            // log.error("保存用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
    }

    @SysLog(MODULE = "sys", REMARK = "更新用户")
    @ApiOperation("更新用户")
    @PostMapping(value = "/update/{id}")
    public Object updateUser(@RequestBody SysNotice entity) {
        try {
            boolean is_succ = sysNoticeService.updateById(entity);
            if (is_succ) {
                return new CommonResult().success();
            }

            // if (sysUserService.updateById(entity)) {
            //     return new CommonResult().success();
            // }
        } catch (Exception e) {
            // log.error("更新用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sys", REMARK = "删除用户")
    @ApiOperation("删除用户")
    @GetMapping(value = "/delete/{id}")
    public Object deleteUser(@ApiParam("用户id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("用户id");
            }
            SysNotice notice = sysNoticeService.getById(id);
            if (notice.getId() != null && notice.getId() == 1) {
                return new CommonResult().paramFailed("管理员账号不能删除");
            }

            QueryWrapper qw = new QueryWrapper<SysNotice>();
            qw.eq("id",id);
            sysNoticeService.remove(qw);
//            boolean is_succ =sysNoticeService.removeById(id);
//            if (is_succ) {
//                return new CommonResult().success();
//            }
        } catch (Exception e) {
            // log.error("删除用户：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }



}

